package pilates.blogmoon.cn.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pilates.blogmoon.cn.api.Result;
import pilates.blogmoon.cn.dto.QueryActionReq;
import pilates.blogmoon.cn.entity.Action;
import pilates.blogmoon.cn.entity.Category;
import pilates.blogmoon.cn.service.ActionService;
import pilates.blogmoon.cn.service.CategoryService;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Copyright:
 * Author:
 * Description:
 * Created by 李林山 on 2024/4/15
 */
@RestController
@RequestMapping("/action")
public class ActionController {

    @Resource
    private ActionService actionService;

    @PostMapping("/create")
    public Result createCategory(@RequestBody Action action) {
        actionService.saveCategory(action);
        return Result.OK();
    }

    @PostMapping("/modify")
    public Result modifyCategory(@RequestBody Action action) {
        actionService.updateCategory(action);
        return Result.OK();
    }

    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable("id") Long id) {
        actionService.removeById(id);
        return Result.OK();
    }

    @PostMapping("/page")
    public Result pageList(@RequestBody QueryActionReq req) {
        LambdaQueryWrapper<Action> wrapper = Wrappers.<Action>lambdaQuery().like(StringUtils.isNotBlank(req.getActionName()),
                        Action::getName, req.getActionName())
                .in(CollectionUtils.isNotEmpty(req.getCategoryId()), Action::getId, req.getCategoryId());
        if (Objects.equals(2, req.getSortType())) {
            wrapper.orderByAsc(Action::getId);
        }
        Page<Action> page = actionService.page(new Page<>(req.getPageNo(), req.getPageSize()), wrapper);
        if (CollectionUtils.isEmpty(page.getRecords())) {
            return Result.OK(page);
        }
        Set<Long> idSet = page.getRecords().stream().map(Action::getId).collect(Collectors.toSet());
        List<Category> categories = actionService.selectCategoryListById(idSet);
        Map<Integer, Category> categoryMap = Maps.newHashMap();
        categoryMap = categories.stream().collect(Collectors.toMap(Category::getId, Function.identity()));

        for (Action action : page.getRecords()) {
            action.setCategoryList(action.getCategoryId().stream().map(categoryMap::get).collect(Collectors.toList()));
        }
        return Result.OK(page);
    }
}
